package cn.com.dhcc.app.pub.core.tag;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;

import org.springframework.util.StringUtils;

import cn.com.dhcc.app.core.tag.CommonTagEl;
import cn.com.dhcc.app.core.util.ReflectionUtil;

/**
 * el 表达式
 *
 * @日期：2013-1-8下午3:01:27
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
public class AppTagEl extends CommonTagEl {
    private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   
    /**
     * 性别code-str转换 useage:${si:sex("1")} or ${si:sex(vo.sex)}
     *
     * @param code
     * @return
     */
    public static String sex(String code) {
        String sex = "";
        if ("1".equals(code)) {
            sex = "男";
        }
        if ("2".equals(code)) {
            sex = "女";
        }
        return sex;
    }

    /**
	 * 格式化文件名称
	 * @param filename
	 * @return
	 */
	public static String formatterFile(String filename) {
		StringBuffer sb=new StringBuffer();
		String[] filenames=filename.split(",");
		String str=null;
		for (int i=0;i<filenames.length;i++) {
			int index=filenames[i].lastIndexOf("/");
			if (index>=0) {
				str=filenames[i].substring(index+1);
			}else{
				str=filenames[i];
			}
			if (i==filenames.length) {
				sb.append(str);
			}else{
				sb.append(str+"<br/>");
			}
		}
		return sb.toString();
	}
	
    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String fmtDate(Date date) {
        String dateStr = "";
        if (date != null) {
            dateStr = df.format(date);
        }
        return dateStr;
    }

    /**
     * 创建下拉框的通用方法
     *
     * @param enumClass
     * @param selectedCode
     * @param excludeCode 排除的code ,号隔开
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    private static String ctoption(Class enumClass, String selectedCode,String excludeCodeStr) {
        EnumSet cts = EnumSet.allOf(enumClass);
        if (StringUtils.hasText(selectedCode))
            selectedCode = selectedCode.trim();
        StringBuilder rp = new StringBuilder();
        List<String> excludeCodes = new ArrayList<String>();
        if(StringUtils.hasText(excludeCodeStr)){
        	excludeCodes.addAll(Arrays.asList(excludeCodeStr.split(",")));
        }
        if (cts != null) {
            for (Iterator<Enum> iterator = cts.iterator(); iterator.hasNext(); ) {
                Enum em = (Enum) iterator.next();
                String code = (String) ReflectionUtil.getFieldValue(em, "code");
                if(excludeCodes.contains(code))continue;
                String name = (String) ReflectionUtil.getFieldValue(em, "name");
                rp.append("<option value=\"").append(code).append("\" ")
                        .append(code.equalsIgnoreCase(selectedCode) ? "selected" : "").append(">").append(name)
                        .append("</option>\r\n");
            }
        }
        return rp.toString();
    }
    /**
     * 创建下拉框的通用方法
     *
     * @param enumClass
     * @param selectedCode
     * @param excludeCode 排除的code ,号隔开
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    private static String ctoptionExt(Class enumClass, String selectedCode,String excludeCodeStr,String extName) {
        EnumSet cts = EnumSet.allOf(enumClass);
        if (StringUtils.hasText(selectedCode))
            selectedCode = selectedCode.trim();
        StringBuilder rp = new StringBuilder();
        List<String> excludeCodes = new ArrayList<String>();
        if(StringUtils.hasText(excludeCodeStr)){
        	excludeCodes.addAll(Arrays.asList(excludeCodeStr.split(",")));
        }
        if (cts != null) {
            for (Iterator<Enum> iterator = cts.iterator(); iterator.hasNext(); ) {
                Enum em = (Enum) iterator.next();
                String code = (String) ReflectionUtil.getFieldValue(em, "code");
                if(excludeCodes.contains(code))continue;
                String name = (String) ReflectionUtil.getFieldValue(em, "name");
                rp.append("<option value=\"").append(code).append("\" ")
                        .append(code.equalsIgnoreCase(selectedCode) ? "selected" : "");
                if(StringUtils.hasText(extName)){
	                String[] extNames = extName.split(",");
	                for (int i = 0; i < extNames.length; i++) {
						rp.append(" "+extNames[i]+"=\""+(String) ReflectionUtil.getFieldValue(em, extNames[i])+"\"");
					}
                }
                rp.append(">").append(name)
                        .append("</option>\r\n");
            }
        }
        return rp.toString();
    }
    /**
     * 创建下拉框的通用方法
     *
     * @param enumClassName 在{@link CT}}里面注册过的码表的class名称
     * @param selectedCode  已选择的code
     * @param excludeCode 排除的code ,号隔开
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String ctoptionExt(String enumClassName, String selectedCode,String excludeCodeStr,String extName) {
        Class enumClass = CT.CodeTablePool.get(enumClassName);
        return ctoptionExt(enumClass, selectedCode,excludeCodeStr,extName);
    }
    /**
     * 创建下拉框的通用方法
     *
     * @param enumClassName 在{@link CT}}里面注册过的码表的class名称
     * @param selectedCode  已选择的code
     * @param excludeCode 排除的code ,号隔开
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String ctoption(String enumClassName, String selectedCode,String excludeCodeStr) {
        Class enumClass = CT.CodeTablePool.get(enumClassName);
        return ctoption(enumClass, selectedCode,excludeCodeStr);
    }
    /**
     * 创建下拉框的通用方法
     *
     * @param enumClassName 在{@link CT}}里面注册过的码表的class名称
     * @param selectedCode  已选择的code
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String ctoption(String enumClassName, String selectedCode) {
        Class enumClass = CT.CodeTablePool.get(enumClassName);
        return ctoption(enumClass, selectedCode,null);
    }
    /**
     * 根据给定的code获取name
     *
     * @param enumClassName
     * @param selectedCode
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static String ctname(String enumClassName, String selectedCode) {
        Class enumClass = CT.CodeTablePool.get(enumClassName);
        EnumSet cts = EnumSet.allOf(enumClass);
        if (cts != null || !StringUtils.hasText(selectedCode)) {
            for (Iterator<Enum> iterator = cts.iterator(); iterator.hasNext(); ) {
                Enum em = (Enum) iterator.next();
                String code = (String) ReflectionUtil.getFieldValue(em, "code");
                if (selectedCode.equalsIgnoreCase(code)) {
                    String name = (String) ReflectionUtil.getFieldValue(em, "name");
                    return name;
                }
            }
        }
        return "";
    }

  
//	public static void main(String[] args) {
//		System.out.println(ctoptionExt("DB_TYPE", "", "","driverName,linkUrl"));
//		System.out.println(ctoption("OWN_SYS_CODE", ""));
//	}
}
